package utils;

import com.itextpdf.kernel.events.Event;
import com.itextpdf.kernel.events.IEventHandler;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import entry.Panel;

/**
 * @Author LS
 * @Date 2023/5/4 17:34
 * @Description
 **/

public class HeaderFooterHandler implements IEventHandler {

    private final Panel panel;

    public HeaderFooterHandler(Panel panel){
        this.panel = panel;
    }

    @Override
    public void handleEvent(Event event) {
        final PdfDocumentEvent pdfDocumentEvent = (PdfDocumentEvent) event;
        final PdfDocument pdfDocument = pdfDocumentEvent.getDocument();
        final Document document = new Document(pdfDocument);
        final PdfPage page = pdfDocumentEvent.getPage();
        // 当前页
        int pageNumber = pdfDocument.getPageNumber(page);
        // 总页面
        int numberOfPages = pdfDocument.getNumberOfPages();
        Paragraph paragraph = new Paragraph(pageNumber + " - " + numberOfPages);

        // 页脚位置
        double footerLeft = panel.getPaperNumberLeft();
        double footerTop = panel.getPaperNumberTop();
        double pagerHeight = panel.getHeight() * 2.83;
        double footerBottom = pagerHeight - footerTop - 15f;
        paragraph.setFontSize(9f);
        paragraph.setFixedPosition(pageNumber, (float) footerLeft, (float) footerBottom, 30f);
        document.add(paragraph);
    }
}
